package tcdx.uap.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tcdx.uap.common.entity.AjaxResult;
import tcdx.uap.common.entity.page.TableDataInfo;
import tcdx.uap.common.utils.Lutils;
import tcdx.uap.common.utils.MapUtils;
import tcdx.uap.common.utils.SqlUtil;
import tcdx.uap.mapper.BaseDBMapper;
import tcdx.uap.mapper.FlowableMapper;
import tcdx.uap.service.BaseDBService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/uap/task")
public class TaskController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(TaskController.class);
    private String prefix = "";


    @Autowired
    private FlowableMapper flowableMapper;

    @Autowired
    private BaseDBService db;

    @Autowired
    BaseDBMapper baseDBMapper;
    @Autowired
    private BaseDBService baseDBService;

    //    @RequiresPermissions("system:flowable:get_flow_mapper")
    @PostMapping("/get_taskviews_cfg")
    @ResponseBody
    public AjaxResult get_taskviews_cfg(@RequestBody Map<String, Object> map)
    {
        //从session中取当前登录人员信息
        Integer menu_id =  (Integer)map.get("menu_id");
        List<Map> taskviews = baseDBService.selectEq("v_taskview",Lutils.genMap("menu_id", menu_id), Lutils.genMap("ord", "asc"));
        Map<String,Object> re = new HashMap<>();
        re.put("taskviews", taskviews);
        //判断是否有配置视图
        if(taskviews.size() > 0){
            //获取搜索表单
            Map r1 = get_taskview_cfg_map(taskviews.get(0));
            re.putAll(r1);
        }
        //找到表名
        return AjaxResult.success("success", re);
    }

    public Map get_taskview_cfg_map(Map taskview){
        Map re=new HashMap();
        //获取表单form
        List formList = baseDBService.selectEq("v_form_field",
                MapUtils.G("form_id", taskview.get("search_form_id")),
                MapUtils.G("ord", "asc"));
        re.put("searchFormItems", formList);
        //获取第一个表格列
        List<Map> curTaskViewColumns =flowableMapper.get_taskview_col_cfg(Lutils.genMap("taskview_id", taskview.get("id")));
        re.put("taskViewColumns", curTaskViewColumns);
        return re;
    }

    @PostMapping("/get_taskview_cfg")
    @ResponseBody
    public AjaxResult get_taskview_cfg(@RequestBody Map<String, Object> map)
    {
        Integer taskview_id = (Integer)map.get("taskview_id");
        List<Map> taskviews = baseDBService.selectEq("v_taskview",Lutils.genMap("id", taskview_id), Lutils.genMap("ord", "asc"));
        Map re =get_taskview_cfg_map(taskviews.get(0));
        return AjaxResult.success("success", re);
    }

    @PostMapping("/get_taskview_columns_config")
    @ResponseBody
    public AjaxResult get_taskview_columns_config(@RequestBody Map<String, Object> map)
    {
        Integer taskview_id = (Integer)map.get("taskview_id");
        List<Map> l =flowableMapper.get_taskview_col_cfg(Lutils.genMap("taskview_id", taskview_id));
        return AjaxResult.success("success", l);
    }

    @PostMapping("/get_taskdata")
    @ResponseBody
    public TableDataInfo get_taskdata(@RequestBody Map<String, Object> map)
    {
        Integer taskview_id = (Integer)map.get("taskview_id");
        //从session中取当前登录人员信息
        Map userInfo = Lutils.genMap("user_name", "luyu1", "user_group", 9);
        //找到主表格与依赖关系
        Map taskview =  baseDBService.selectEq("v_taskview", MapUtils.G("id", taskview_id)).get(0);
        String main_datatable = taskview.get("main_datatable").toString();
        String main_table_id = main_datatable.substring(11,main_datatable.length());
        List<Map> joinDataTables = flowableMapper.get_datatable_recurive_relations(Lutils.genMap("main_datatable", main_datatable));
//        List<String> foreign_tbl_id= joinDataTables.stream().filter(o->!o.get("foreign_table_name").equals(main_tablename))
//                .map(o->o.get("foreign_table_name").toString().substring(11,o.get("foreign_table_name").toString().length())).collect(Collectors.toList());
        //找到要显示的列
        List<Map> selColumnsList = baseDBService.selectEq("v_taskview_columns", Lutils.genList("table_id","table_col_id", "field"), Lutils.genMap("taskview_id", taskview_id));
        map.put("main_datatable", main_datatable);
        map.put("main_table_id", main_table_id);
        map.put("joinDataTables", joinDataTables);
        map.put("selColumnsList", selColumnsList);
        //添加权限筛选条件
        ((List)((Map)map.get("obj_c1")).get("cas")).add(SqlUtil.eq("assignee_", userInfo.get("user_name").toString()));
        //根据类型查询
        List list = null;
        if(map.get("orderByColumn")==null){
            map.put("orderByColumn","id_");
            map.put("isAsc", "desc");
        }
        startPage(map);
        list = flowableMapper.get_taskdata(map);
        return getDataTable(list);
    }


}
